package com.ks.base.controller;

import com.ks.core.core.Result;
import com.ks.core.file.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author kingdee
 * @date 2020/11/17 15:09
 */
@RestController
@RequestMapping("/base")
@Api(value = "基础服务模块",description = "基础服务模块")
public class BaseController {

    @GetMapping("/getBase")
    public Object Base(){
        return "这是Base服务";
    }


    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ApiOperation(value = "基础服务图片上传接口",notes = "基础服务图片上传接口")
    public Object upload(@RequestPart("file") MultipartFile picture) throws Exception {
        int begin = picture.getOriginalFilename().indexOf(".");
        int last = picture.getOriginalFilename().length();
        //获得文件后缀名
        String filename = picture.getOriginalFilename().substring(begin, last);
        if (filename.equals(".jpg")) {
            String pic = UploadUtil.uploadFileToOSS(picture, 1, "ks/img/");
            return Result.success(pic);
        }
        String pic = UploadUtil.uploadFileToOSS(picture, 1, "ks/pdf/");
        return Result.success(pic);
    }



    @RequestMapping(method = RequestMethod.POST, path = "/uploadVideo")
    @ApiOperation(value = "基础服务图片上传接口",notes = "基础服务视频上传接口")
    public Object uploadVideo(@RequestPart("file") MultipartFile picture) throws Exception {
        int begin = picture.getOriginalFilename().indexOf(".");
        int last = picture.getOriginalFilename().length();
        //获得文件后缀名
        String filename = picture.getOriginalFilename().substring(begin, last);
        String pic = UploadUtil.uploadFileToOSS(picture, 1, "ks/video/");
        return Result.success(pic);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/uploadFile")
    @ApiOperation(value = "基础服务文件上传接口",notes = "基础服务文件上传接口")
    public Object uploadFile(@RequestPart("file") MultipartFile picture) throws Exception {
        int begin = picture.getOriginalFilename().indexOf(".");
        int last = picture.getOriginalFilename().length();
        //获得文件后缀名
        String filename = picture.getOriginalFilename().substring(begin, last);
        if (filename.equals(".txt")) {
            String pic = UploadUtil.uploadFileToOSS(picture, 1, "ks/txt/");
            return Result.success(pic);
        }
        String pic = UploadUtil.uploadFileToOSS(picture, 1, "ks/file/");
        return Result.success(pic);
    }
}
